#include "File.h" 
#include <string.h> 
#include <time.h>

int read_file(char* filename,char* key,char* value) 
{ 
    int flag=0;
    char buffer[SIZE];
    FILE *file=fopen(filename,"r");

//    try
//    {
        if(file==NULL)
        {
            flag=1;
            //throw(flag);
        }
        else
        {
            while(fgets(buffer,SIZE,file)!=NULL)
            {
                int i=0,j=0,len=strlen(key);
                while(buffer[i]!='\0')
                {
                    if(buffer[i]=='$'&&buffer[i+len+1]=='=')
                    {
                        j=i+len+2;
                        while(buffer[j]!='\0'&&buffer[j]!=';')
                        {
                            int h=0;
                            if(buffer[i+1]==key[i])
                            {
                                //printf("%c",buffer[j]);
                                value[j-i-len-2]=buffer[j];
                            }
                            j++;
                        }
                        break;
                    }
                    else if(buffer[i]=='/'&&buffer[i+1]=='/'||buffer[i]==';')
                    {
                        break;
                        //comment
                    }


                    i++;
                }
            }
        }
//    }
//    catch(Exception)
//    {
        if(flag==1){
        flag=2;
        fclose(file);
        //printf("can't open file %s",filename);
        printf("can't open file %s",filename);
        //exit(1);
        return flag;
        }
//    }
    fflush(file);
    fclose(file);
    return flag;

} 

int write_file(char* filename,char* key,char* value) 
{ 
    int flag=0;
    FILE* file;
    file=fopen(filename,"a");
//    try
//    {
        if(file==NULL)
        {
            flag=1;
            //throw(flag);
        }
        else
            fprintf(file,"$%s=%s\n",key,value);
//    }
//    catch(Exception)
//    {
        if(flag==1){
            printf("Can't write file %s",filename);

        //printf("Can't write file %s",filename);
        //exit(1);
        return flag;
        }
//    }
    fflush(file);
    fclose(file);
    return flag;
} 

int write_section(char* filename,char* section) 
{ 
    int flag=0;
    FILE* file=NULL;
//    try
//    {
        file=fopen(filename,"a");
        if(file!=NULL)
        {
            fprintf(file,"[%s]\n",section);
        }
        else
        {
            flag=1;
            //throw(flag);
        }
//    }
//    catch(Exception)
//    {
        if(flag==1){
            printf("can't open file %s",filename);
            return flag;
        }
//        printf("can't open file %s",filename);
//        exit(0);
//    }
    fflush(file);
    fclose(file);
    return flag;
} 

int release() 
{ 
    int flag=1;
    return flag;
} 

int read_section(char* filename) 
{ 
    return 0;
} 

void getVersion(char* value) 
{ 
    char version[128]="2009//05//01";
    int i=0;
    for(i=0;i<strlen(version);i++)
    {
        value[i]=version[i];
    }

} 
/************************************************************************** 
void main() 
{ 
 char* str=NULL;
 char author[120];
 char buffer[128];
 char buffer1[128];
 char buffer2[128];
 read_file("F:\\exercise\\C++!C\\sys.ini","password",buffer);
 read_file("F:\\exercise\\C++!C\\sys.ini","username",buffer1);
 read_file("F:\\exercise\\C++!C\\sys.ini","driver",buffer2);
 printf("password=%s\n",buffer);
 printf("\n");
 printf("username=%s\n",buffer1);
 printf("\n");
 printf("driver=%s\n",buffer2);
 getAuthor(author);
 printf("\n");
 printf("author=%s",author);
 release();
}
*/
